package framework.sepal4java.common.struct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CtxServiceResult implements ServiceResult{
    private Map<String, Long> result;

    public CtxServiceResult() {
        result = new HashMap<>();
    }

    public CtxServiceResult(long activeRuleNum, long receivedCtxNum, long ckeckedCtxNum, long resolvedCtxNum) {
        result = new HashMap<>();
        result.put("active_rule_number", activeRuleNum);
        result.put("received_ctx_number", receivedCtxNum);
        result.put("checked_ctx_number", ckeckedCtxNum);
        result.put("resolved_ctx_number", resolvedCtxNum);
    }

    @Override
    public String toJSONString() {
        return toString();
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jo = new JSONObject();
        jo.putAll(result);
        return jo;
    }

    @Override
    public String toString() {
        JSONObject jo = new JSONObject();
        jo.putAll(result);
        return jo.toJSONString();
    }

    public static CtxServiceResult fromJSONString(String json) {
        CtxServiceResult res = new CtxServiceResult();
        JSONObject jo = JSON.parseObject(json);
        for (Map.Entry<String, Object> entry : jo.entrySet()) {
            res.result.put(entry.getKey(), Long.parseLong(entry.getValue().toString()));
        }
        return res;
    }

    public long getActiveRuleNum() {
        return result.getOrDefault("active_rule_number", 0L);
    }

    public long getReceivedCtxNum() {
        return result.getOrDefault("received_ctx_number", 0L);
    }

    public long getCkeckedCtxNum() {
        return result.getOrDefault("checked_ctx_number", 0L);
    }

    public long getResolvedCtxNum() {
        return result.getOrDefault("resolved_ctx_number", 0L);
    }
}
